﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ErrorHandling
{
    public partial class Error : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            var exception = Session["LastException"] as Exception;
            if (exception != null)
            {
                ErrorStackTrace.Text = exception.StackTrace;

                if (exception.InnerException != null)
                {
                    InnerExceptionPanel.Visible = true;
                    InnerErrorStackTrace.Text = exception.InnerException.Message;
                }
            }
            else
            {
                ErrorStackTrace.Text = "No extra data";
            }

            var lastError = Session["LastError"] as string;
            var lastErrorLocation = Session["LastErrorLocation"] as Uri;

            Session.Remove("LastError");
            Session.Remove("LastErrorLocation");

            ErrorUrl.Text = lastErrorLocation == null ? "Unknown Location" : lastErrorLocation.AbsoluteUri;
            ErrorDetails.Text = string.IsNullOrEmpty(lastError) ? "Unkown Error" : lastError;
        }
    }
}